﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FixedPropertyManagement.Models;

namespace FixedPropertyManagement.aspxs
{
    public partial class getPassword : System.Web.UI.Page
    {
        PropertyMangementEntities3 db = new PropertyMangementEntities3();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //修改
        protected void btnChange_Click(object sender, EventArgs e)
        {
            
            string id = txtNum.Text.Trim();
            string pwd = txtPassword.Text.Trim();  
            string celpwd = celPassword.Text.Trim();
            string tell = txtTell.Text.Trim();
            string code = txtcode.Text.Trim();
            if (id == "")
            {
                Response.Write("<script>alert('账号不能为空！')</script>");
                txtNum.Focus();
                btnGetTellCode.Text = "获取验证码";
                return;
            }           
            if (pwd.Equals("") || celpwd.Equals(""))
            {
                Response.Write("<script>alert('密码不能为空,请补充！')</script>");
                txtPassword.Focus();
                btnGetTellCode.Text = "获取验证码";
                return;
            }
            if (tell == "")
            {
                Response.Write("<script>alert('电话不能为空！')</script>");
                txtTell.Focus();
                btnGetTellCode.Text = "获取验证码";
                return;
            }
            if (code == "" || code=="获取验证码")
            {
                Response.Write("<script>alert('手机验证不能为空或请获取验证码！')</script>");
                txtcode.Focus();
                btnGetTellCode.Text = "获取验证码";
                return;
            }
            if (!pwd.Equals(celpwd))
            {
                Response.Write("<script>alert('密码不一致！')</script>");
                txtPassword.Focus();
                btnGetTellCode.Text = "获取验证码";
                return;
            }
            if (!code.Equals(btnGetTellCode.Text))
            {
                Response.Write("<script>alert('手机验证密码不正确！')</script>");
                txtcode.Focus();
                string[] arr = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
                var str = "";
                Random r = new Random();
                for (var i = 0; i < 4; i++)
                {                  
                    str += arr[r.Next(0, 20)];
                }
                btnGetTellCode.Text = str;
                return;
            }
            var result = db.Employees.Where(em => em.EmpId.ToString() == id).FirstOrDefault();
            if (result == null)
            {
                Response.Write("<script>alert('系统中没有该账号！')</script>");
            }
            else
            {
                if(result.Tell == tell)
                {
                    result.Password = pwd;
                    try
                    {
                        db.SaveChanges();
                        Response.Write("<script>alert('修改成功！')</script>");
                        txtcode.Text = "";
                        btnGetTellCode.Text = "获取验证码";
                        txtTell.Text = "";
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }else
                {
                    Response.Write("<script>alert('员工的电话不正确，修改失败！')</script>");
                    txtTell.Focus();
                }
                
            }
        }
        //返回登录界面
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("login.aspx");
        }
    }
}